#include "ESSearch.hpp"

namespace chatroom
{
    class ESclientFactory{
        public:
            static std::shared_ptr<elasticlient::Client> create(const std::vector<std::string> &hostUrlList)
            {
                return std::make_shared<elasticlient::Client>(hostUrlList);
            }
    };
    class ESuser()
    {
        public:
            using Ptr = std::shared_ptr<ESuser>;
            ESuser( std::shared_ptr<elasticlient::Client>& es_client):_es_client(es_client){}
            //创建用户索引
            bool index(){
                bool ret = ESIndex(_es_client,"user")
                           .append(_usrid,"keyword","standard",true)
                           .append(_nickname)
                           .append(_desc,"text","standard",false)
                           .append(_phone,"keyword","standard",true)
                           .append(_avid,"keyword","standad",false)
                           .create();
                if(ret==false)
                {
                    Logger_ERROR("创建索引失败");
                    return false;
                }
                Logger_INFO("创建索引成功");
                return true;
            }
            //往ES字段中添加或者更新数据
            bool appendData(
                const std::string& usrid,
                const std::string& nickname,
                const std::string& desc,
                const std::string& phone,
                const std::string& avid){
                    bool ret = ESInsert(_es_client,"user")
                                .append(_usrid,usrid)
                                .append(_nickname,nickname)
                                .append(_phone,phone)
                                .append(_desc,desc)
                                .append(_avid,avid)
                                .insert(uid);
                    if(ret==false)
                    {
                        Logger_ERROR("插入数据失败");
                        return false;
                    }
                    Logger_INFO("插入数据成功");
                    return true;
            }
            //搜索数据
            vector<chatroom::User> search(const std::vector<std::string>& uidlist){
                vector<chatroom::User> res;
                Json::Value json_user = ESSearch(_es_client,"user")
                                        .append_must_match("phone.keyword",key)
                                        .append_must_match("user_id.keyword",key)
                                        .append_must_match("nickname",key)
                                        .append_must_not_term("user_id.keyword",uidlist)
                                        .search();
                if(!json_user.isArray())
                {
                    Logger_ERROR("搜索数据失败,结果不是一个数组");
                    return res;
                }
                Logger_INFO("一共检索到{}"json_user.size());
                for(int i = 0; i < json_user.size(); i++)
                {
                    chatroom::User user;
                    user.set_user_id(json_user[i]["_source"][_user_id].as_String());
                    user.set_phone(json_user[i]["_source"][_phone].as_String());
                    user.set_nickname(json_user[i]["_source"][_nickname].as_String());
                    user.set_desc(json_user[i]["_source"][_desc].as_String());
                    user.set_avatar_id(json_user[i]["_source"][_avid].as_String()); 
                    res.push_back(user);  
                }
                return res;                     
            }
        private:
            const std::string _usrid = "user_id";
            const std::string _nickname = "nickname";
            const std::string _phone = "phone";
            const std::string _desc = "description";
            const std::string _avid = "avatar_id";
            std::shared_ptr<elasticlient::Client> _es_client;
    };

    class ESMessage()
    {
        public:
            using Ptr = std::shared_ptr<elasticlient::Client>;
            ESMessage(std::shared_ptr<elasticlient::Client> es_client):_es_client(es_client){}
            bool index(){
                bool ret = ESIndex(_es_client,"message")
                            .append(chat_session_id,"keyword","standard",true)
                            .append(message_id,"keyword","standard",false)
                            .append(send_user_id,"keyword","standard",false)
                            .append("ctime","long","standard",false)
                            .append(content)
                            .create();
                if(!ret)
                {
                    Logger_ERROR("创建消息索引失败");
                    return false;
                }
                return true;
            }
            bool appendData(const std::string& chat_session_id,
            const std::string& message_id,
            const std::string& send_user_id,
            const int64_t ctime,
            const std::string& content){
                bool ret = ESInsert(_es_client,"message")
                           .append("chat_session_id",chat_session_id)
                           .append("message_id",message_id)
                           .append("send_user_id",send_user_id)
                           .append("ctime",ctime)
                           .append("content",content)
                           .insert(message_id);
                if(!ret)
                {
                    Logger_ERROR("插入信息失败");
                    return false;
                }
                return true;
            }
            bool remove(const std::string& message_id){
                bool ret = ESRemove(_es_client,"message").remove(message_id);
                if(!ret)
                {
                    Logger_ERROR("移除消息数据失败");
                    return false;
                }
                return true;
            }
            std::vector<chatroom::message> search(const std::string& ssid,const std::string& key){
                vector<chatroom::message> res;
                Json::Value json_message = ESSearch(_es_client,"message")
                                        .append_must_term("chat_session_id",ssid)
                                        .append_must_match("content",key)
                                        .search();
                if(!json_message.isArray())
                {
                    Logger_ERROR("搜索数据失败,结果不是一个数组");
                    return res;
                }
                Logger_INFO("一共检索到{}"json_message.size());
                for(int i = 0; i < json_message.size(); i++)
                {
                    chatroom::message msg;
                    msg.set_send_user_id(json_user[i]["_source"][send_user_id].as_String());
                    msg.set_message_id(json_user[i]["_source"][message_id].as_String());
                    msg.set_chat_session_id(json_user[i]["_source"][chat_session_id].as_String());
                    msg.set_content(json_user[i]["_source"][content].as_String());
                    msg.set_created_time(json_user[i]["_source"][
                        boost::posix_time::ptime ctime()
                    ].as_Int64()); 
                    res.push_back(msg);  
                }
                return res;  
            }
        private:
            std::string chat_session_id = "chat_session_id";
            std::string message_id = "message_id";
            std::string content ="content";
            std::string send_user_id = "send_user_id";
            std::shared_ptr<elasticlient::Client> _es_client;
    };
}